import router from './router/index'
import store from './store/index'
// import { resetRouter } from '@/router'
router.beforeEach(async(to, from, next) => {
  const token = store.state.user.token
  if (token) {
    store.dispatch('echarts/getAgName')
    // 已登录 --- 判断跳转的页面是否为登录页，是则跳转到首页否则放行
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.id) {
        await store.dispatch('user/getUser')
        // console.log(res)
        const menus = store.state.user.menus
        const routes = await store.dispatch('permission/filterRoutes', menus)
        // console.log(routes)
        router.addRoutes([...routes, { path: '*', redirect: '/404', hidden: true }])
        next(to.path)
      }
      next() // 放行
    }
  } else {
    const writeArr = ['/login', '/404']
    if (writeArr.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})
